package com.ruoyi.web.controller.tool;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * @program: football_manage
 * @description:
 * @author: xiesm
 * @create: 2021-10-27 17:23
 **/
@Slf4j
@RestController
@RequestMapping("/callback")
public class CallbackController {

    //gunsApi/auth/dycw/1277/2122/1f1dc4e842e04c0c9d6447edb98a55e0"
    @SneakyThrows
    @RequestMapping("/crash/{apiId}/{tpaId}/{reqId}")
    public void execute2(@PathVariable String apiId,
                         @PathVariable String tpaId,
                         @PathVariable String reqId,
                         @RequestBody String params,
                         HttpServletResponse response) {
        execute(apiId, tpaId, reqId, params, response);
    }

    @SneakyThrows
    @RequestMapping("/maintenance/{apiId}/{tpaId}/{reqId}")
    public void execute(@PathVariable String apiId,
                        @PathVariable String tpaId,
                        @PathVariable String reqId,
                        @RequestBody String params,
                        HttpServletResponse response) {
        JSONObject jsonObject = new JSONObject();
        log.info(params);
        String[] strs = params.split("&");
        for (int i = 0; i < strs.length; i++) {
            String[] s = strs[i].split("=");
            if (s.length > 1) {
                jsonObject.put(s[0], s[1]);
            } else {
                jsonObject.put(s[0], null);
            }
        }
        String status = jsonObject.getString("status");
        String content = jsonObject.getString("content");
        log.info("{},{},{},{},{}", apiId, tpaId, reqId, status, content);
        ServletOutputStream out = response.getOutputStream();
        try {
            log.info("第一车网 {},{},{},{}, 接口回调数据 {}", apiId, tpaId, reqId, status, content);
            out.print("ok");
        } catch (Exception exception) {
            log.error("第一车网回调异常", exception);
            out.print("error");
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @SneakyThrows
    @RequestMapping("/maintenance")
    public JSONObject execute(@RequestBody String requestBody) {
        log.info(requestBody);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "200");
        return jsonObject;
    }

    @SneakyThrows
    @RequestMapping("/accident")
    public JSONObject accident(@RequestBody String requestBody) {
        log.info(requestBody);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        return jsonObject;
    }

    public static void main(String[] args) {
        String s = FileUtil.readString("D:\\乱七八糟\\json0128.txt", Charset.defaultCharset());
        JSONObject jsonObject = JSONObject.parseObject(s);
        JSONObject encrypt = jsonObject.getJSONObject("encrypt");
        System.out.println(encrypt);
    }

    @SneakyThrows
    @RequestMapping("/dycw2/{apiId}/{tpaId}/{reqId}")
    public JSONObject dycw2(@PathVariable String apiId,
                            @PathVariable String tpaId,
                            @PathVariable String reqId,
                            String OrderId) {
        JSONObject jsonObject = new JSONObject();
        log.info("{},{},{},{}", apiId, tpaId, reqId, OrderId);

        jsonObject.put("Message", "String content");
        jsonObject.put("ResponseStatus", 1);
        return jsonObject;
    }

    @SneakyThrows
    @RequestMapping("/dycw3/{apiId}/{tpaId}/{reqId}")
    public JSONObject dycw3(@PathVariable String apiId,
                            @PathVariable String tpaId,
                            @PathVariable String reqId,
                            String OrderId) {
        JSONObject jsonObject = new JSONObject();
        log.info("{},{},{},{}", apiId, tpaId, reqId, OrderId);

        jsonObject.put("Message", "String content");
        jsonObject.put("ResponseStatus", 1);
        return jsonObject;
    }

    @SneakyThrows
    @RequestMapping("/qx/{apiId}/{tpaId}/{reqId}")
    public JSONObject qx(@PathVariable String apiId,
                         @PathVariable String tpaId,
                         @PathVariable String reqId,
                         @RequestBody String params) {
        JSONObject jsonObject = new JSONObject();
        log.info("{},{},{},{}", apiId, tpaId, reqId, params);

        jsonObject.put("code", 0);
        return jsonObject;
    }
}